import {
    fetchCoinFlowList
} from "../../api/modules/payment";
import {
    getPlayletLikeList
} from "../../api/modules/play";
import {
    APP_ID,
    CLIENT,
    PlATFORM
} from "../../config";
import {
    loginController
} from "../../utils/login";
import {
    accSub
} from "../../utils/number.utils";
import eventBus from "../../utils/eventBus";
import EventName from "../../config/eventName";
Page({
    data: {
        isDeleteModel: false,
        list: [],
        bottomLoading: false
    },
    needChange: false,
    loading: false,
    pageable: {
        total: 0,
        pageSize: 9,
        pageNum: 1
    },
    onShow() {
        if (this.needChange) {
            this.fetchData(1)
            this.needChange = false;
        }
    },
    onLoad: async function() {
        this.fetchData(1)
        eventBus.on(EventName.LIKE_CHANGE, this.handleLikeChange)

    },
    onUnload() {
        eventBus.off(EventName.LIKE_CHANGE, this.handleLikeChange)
       
    },
    handleLikeChange() {
        this.needChange=true;
    },
    onPullDownRefresh() {
        this.fetchData(1, () => {
            tt.stopPullDownRefresh({});
        })
    },
    onReachBottom() {
        if (this.data.list.length >= this.pageable.total) return;
        this.fetchData(this.pageable.pageNum + 1);
    },
    async fetchData(pageNum, cb) {
        if (this.loading) return;
        this.loading = true;
        this.setData({
            bottomLoading: pageNum !== 1
        })
        try {
            await loginController.login()
            const {
                data
            } = await getPlayletLikeList({
                pageNum,
                pageSize: this.pageable.pageSize,
                platform: PlATFORM,
                "app-id": APP_ID,
                "client": CLIENT
            });
            let {
                list,
                ...pageable
            } = data;
            this.pageable = pageable;
            // list = (list || []).map(v=>{
            //   return {
            //     label:v.beforeAmount>v.afterAmount?"消费":"充值",
            //     num:accSub(v.afterAmount,v.beforeAmount),
            //     createdAt:v.createdAt
            //   }
            // });

            this.setData({
                list: pageNum === 1 ? list : this.data.list.concat(list)
            })

            if (this.data.list.length >= this.pageable.total && pageNum !== 1) {
                tt.showToast({
                    title: '到底了～～'
                });
            }
        } catch {

        } finally {
            this.loading = false;
            this.setData({
                bottomLoading: false
            })
            typeof cb === 'function' && cb()
        }

    },
    handleTap(e) {
        let {
            goodsId,
            episodeNum,
            playletId
        } = e.currentTarget.dataset.likeItem;
        tt.navigateTo({
            url: `/pages/videoPlayer/videoPlayer?goodsId=${goodsId}&eNum=${episodeNum}`
        });

    }
})